<template>
    <div>
      <h2>编辑签约信息</h2>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>居民信息</span>
          </div>
          <div>
            <el-form :inline="true" :model="residents" class="demo-form-inline">
              <el-form-item label="姓名">
                <el-input v-model="residents.name" placeholder="姓名" disabled></el-input>
              </el-form-item>
              <el-form-item label="身份证号">
                <el-input v-model="residents.idName" placeholder="身份证号"></el-input>
              </el-form-item>
              <el-form-item label="身份证号">
                <el-select v-model="residents.sex" placeholder="请选择">
                  <el-option label="男" value="男"></el-option>
                  <el-option label="女" value="女"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <el-form :inline="true" :model="residents" class="demo-form-inline">
              <el-form-item label="出生年月">
                <el-date-picker
                    v-model="residents.dateOfBirth"
                    type="date"
                    placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="联系电话">
                <el-input v-model="residents.phone" placeholder="身份证号"></el-input>
              </el-form-item>
              <el-form-item label="现居地">
                <el-input v-model="residents.address" placeholder="现居地"></el-input>
              </el-form-item>
            </el-form>
            <el-form :inline="true" :model="residents" class="demo-form-inline">
              <el-form-item label="居民标签">
                <el-select v-model="residents.residentLabel" placeholder="请选择">
                  <el-option label="慢病护理" value="慢病护理"></el-option>
                  <el-option label="高血压" value="高血压"></el-option>
                  <el-option label="高血糖" value="高血糖"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <el-form :inline="true" :model="residents" class="demo-form-inline">
              <el-form-item label="家庭成员">
                {{ familyMemberList.length > 0? familyMemberList.map(item => `${item.name}`).join(', ') : 'No names available' }}
                <el-link type="primary" @click="addFamilyUp()">+关联家庭成员</el-link>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>签约信息</span>
        </div>
        <div>
          <el-form :inline="true" :model="SigningInfo" class="demo-form-inline">
            <el-form-item label="签约编号">
              {{SigningInfo.number}}
            </el-form-item>
            <el-form-item label="签约状态">
              {{SigningInfo.status===0?'待审核':SigningInfo.status===1?'待支付':this.SigningInfo.status===2?'已驳回':this.SigningInfo.status===3?'生效中':'已过期'}}
            </el-form-item>
            <el-form-item label="签约机构">
              <el-select v-model="SigningInfo.institutionId" placeholder="请选择" clearable>
                <el-option
                    v-for="item in InstitutionList"
                    :label="item.name"
                    :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-form :inline="true" :model="SigningInfo" class="demo-form-inline">
            <el-form-item label="签约团队">
              <el-select v-model="SigningInfo.teamId" placeholder="请选择" clearable>
                <el-option
                    v-for="item in TeamList"
                    :label="item.name"
                    :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="签约医生">
              <el-select v-model="SigningInfo.doctorId" placeholder="请选择">
                <el-option
                    v-for="item in DoctorList"
                    :label="item.doctorName"
                    :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="服务包">
              <el-select v-model="SigningInfo.serviceId" placeholder="请选择" clearable @change="getServiceList()">
                <el-option
                    v-for="item in ServiceList"
                    :label="item.name"
                    :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
            <el-form :inline="true" :model="SigningInfo" class="demo-form-inline">
              <el-form-item label="签约类型">{{ SigningInfo.contractType}}</el-form-item>
              <el-form-item label="签约周期">{{SigningInfo.signingCycle}}</el-form-item>
              <el-form-item label="费用">{{SigningInfo.cost}}</el-form-item>
            </el-form>
          <el-form :inline="true" :model="SigningInfo" class="demo-form-inline">
            <el-form-item label="申请时间">
                {{SigningInfo.applicationTime}}
            </el-form-item>
            <el-form-item label="生效日期">
              <el-date-picker
                  v-model="SigningInfo.effectiveDate"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-form>
          <el-form :inline="true" :model="SigningInfo" class="demo-form-inline">
            <el-form-item label="备注">
              <el-input
                  type="textarea"
                  :rows="2"
                  placeholder="请输入内容"
                  v-model="SigningInfo.remarks">
              </el-input>
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="updateEdit()">保存</el-button>
          <el-button @click="returnPending()">返回</el-button>
        </div>
      </el-card>
      <el-dialog title="关联家庭成员" :visible.sync="dialogFormVisibleFamily">
        <el-form :model="familyMember">
          <el-form-item label="选择成员">
            <el-select v-model="familyMember.id" placeholder="请选择">
              <el-option
                  v-for="item in FamilyMemberAll"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择关系">
            <el-select v-model="familyMember.familyRelationship" placeholder="请选择">
                <el-option label="父母" value="父母"></el-option>
                <el-option label="配偶" value="配偶"></el-option>
                <el-option label="姐妹" value="姐妹"></el-option>
                <el-option label="兄弟" value="兄弟"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleFamily = false">取 消</el-button>
          <el-button type="primary" @click="addFamily()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
import axios from "axios";

export default {
  name: "EditContractInformation",
  data() {
    return {
      pendingSigningId: null,
      residents: {},
      familyMemberList: [],
      FamilyMemberList: [],
      dialogFormVisibleFamily: false,
      FamilyMemberAll: [],
      familyMember: {},
      MechanismList: [],
      DoctorTeamList: [],
      ServicePackageList: [],
      DoctorList: [],
      SigningInfo:{},
      InstitutionList: [],
      TeamList: [],
      ServiceList: []
    }
  },
  created() {
    this.pendingSigningId=this.$route.query.pendingSigningId
    this.findByPendIdSigningInfo()
    this.findByPendIdResidents()
    // this.findAllByPendIdFamilyMember()
    // this.findAllMechanism()
    // this.findAllDoctorTeam()
    // this.findAllServicePackage()
    this.findAllDoctor()
    this.findAllInstitution()
    this.findAllZlTeam()
    this.findAllZlService()
  },
  methods: {
    getServiceList(){
      this.ServiceList.forEach(item=>{
        if (item.id===this.SigningInfo.serviceId){
          this.SigningInfo.cost=item.price
        }
      })
    },
    updateEdit(){
      axios.post(`http://localhost:8201/signingInfo/updateSigningInfo`,{pendingSigningId:this.pendingSigningId,
        signingInfo:this.SigningInfo,residents:this.residents}).then(res=>{
        this.$message(res.data.message)
        if (this.SigningInfo.status===3||this.SigningInfo.status===4){
          this.$router.push({path: '/qianyue/jilu/qjilu'})
        }else {
          //返回PendingSigning页面
          this.$router.push({path: '/qianyue/daichuli'})
        }

      })
    },
    returnPending(){
      //返回PendingSigning页面
      if (this.SigningInfo.status===3||this.SigningInfo.status===4){
        this.$router.push({path: '/qianyue/jilu/qjilu'})
      }else {
        //返回PendingSigning页面
        this.$router.push({path: '/qianyue/daichuli'})
      }
    },
    findAllInstitution(){
      axios.get("http://localhost:8300/phl/institution/findName").then(res=>{
        this.InstitutionList=res.data.data
      })
    },
    findAllZlTeam(){
      axios.get("http://localhost:8300/phl/team/findTeam").then(res=>{
        this.TeamList=res.data.data
      })
    },
    findAllZlService(){
      axios.get("http://localhost:8300/phl/service/findAllServiceList").then(res=>{
        this.ServiceList=res.data.data
      })
    },
    findAllDoctor(){
      axios.get("http://localhost:8300/phl/doctor/findDoctor").then(res=>{
        this.DoctorList=res.data.data
      })
    },
    // findAllServicePackage(){
    //   axios.get("http://localhost:8201/servicePackage/").then(res=>{
    //     this.ServicePackageList=res.data.data
    //   })
    // },
    // findAllDoctorTeam(){
    //   axios.get("http://localhost:8201/doctorTeam/").then(res=>{
    //     this.DoctorTeamList=res.data.data
    //   })
    // },
    // findAllMechanism(){
    //   axios.get("http://localhost:8201/mechanism/").then(res=>{
    //     this.MechanismList=res.data.data
    //   })
    // },
    addFamily(){
      axios.post(`http://localhost:8801/chengyuan/addFamilyMember`,{
        juminId:this.SigningInfo.juminId,
        two:this.familyMember.id,
        familyRelationship:this.familyMember.familyRelationship
      }).then(res=>{
        if (res.data.code==200){
          this.$message.success('添加关联人成功')
        }else {
          this.$message.error('添加失败')
        }
        this.dialogFormVisibleFamily=false
        this.findByPendIdResidents()
      })
    },
    findAllFamily(){
      axios.post("http://localhost:8801/jumin/findAll").then(res=>{
        this.FamilyMemberAll=res.data
      })
    },
    addFamilyUp(){
      this.findAllFamily()
      this.dialogFormVisibleFamily=true
    },
    // findAllByPendIdFamilyMember(){
    //   axios.get(`http://localhost:8201/familyMember/findByPendIdFamilyMember?pendingSigningId=${this.pendingSigningId}`).then(res=>{
    //       this.FamilyMemberList=res.data.data
    //   })
    // },
    findByPendIdSigningInfo(){
      axios.get(`http://localhost:8201/signingInfo/findByPendIdSigningInfo?pendingSigningId=${this.pendingSigningId}`).then(res=>{
        this.SigningInfo=res.data.data
      })
    },
    findByPendIdResidents(){
      axios.get(`http://localhost:8201/residents/findByPendIdResidents?pendingSigningId=${this.pendingSigningId}`).then(res=>{
        this.residents=res.data.data
        this.familyMemberList=res.data.data.chengyuanList
      })
    }
  }
}
</script>

<style scoped>

</style>